package com.pink.controller;

import com.pink.entity.dto.UserDto;
import com.pink.entity.query.UserQuery;
import com.pink.entity.vo.ResponseVo;
import com.pink.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/admin/user")
@Validated
public class UserController extends ABaseController{

    // 注入UserService
    @Resource
    UserService userService;

    // 加载数据列表
    @RequestMapping("/loadDataList")
    public ResponseVo<UserQuery> loadDataList(
            // 页码，默认为1
            @RequestParam(value = "PageNo", defaultValue = "1") int pageNo,
            // 每页大小，默认为15
            @RequestParam(value = "PageSize", defaultValue = "15") int pageSize,
            String username,Integer userRole) {
        return ResponseVo.success(userService.findUserQueryList(pageNo, pageSize,username,userRole));
    }

    //删除用户
    @RequestMapping("/deleteUser")
    public ResponseVo<String> deleteUser(@NotNull Integer id){
        return ResponseVo.success(userService.deleteUser(id));
    }

    //修改用户
    @RequestMapping("/updateUser")
    public ResponseVo<String> updateUser(@NotNull UserDto user){
        return ResponseVo.success(userService.updateUser(user));
    }

    //添加用户
    @RequestMapping("/addUser")
    public ResponseVo<String> addUser(@NotNull UserDto user){
        return ResponseVo.success(userService.addUser(user));
    }

    //修改个人密码
    @RequestMapping("/updatePassword")
    public ResponseVo<String> updatePassword(@NotNull UserDto user,String repPassword){
        UserDto userQue = getTokenUserDto();
        return ResponseVo.success(userService.updatePassword(userQue.getUserId(),user,repPassword));
    }
}
